clear all
clear matrix
version 13
capture log close
set more off, permanently

*-------------------------------------------------------------------------------
* Options	
*-------------------------------------------------------------------------------	
	* For Heiko:
	global main		= "\\iab.baintern.de\DFS\017\Ablagen\D01700-Data\N2066017\Datenaustausch\GradAB\Schnitzlein_Stueber\Stueber\CADAL_Flow_Dataset\Federica"
	global data_orig= "N:\Ablagen\D01700-IAB-Projekte\D01700-CADAL_Flow_Dataset\package"
	global data_proc= "$main"+"\"+"temp"
	global log   	= "$main"+"\"+"log"
	global do		= "$main"+"\"+"do"
	global data_add = "$main"+"\"+"data"

*-------------------------------------------------------------------------------
cd "$main"
*-------------------------------------------------------------------------------
log using "$log/firmlevelpersistence28052020", text replace
********************************************************************************
use "$data_proc\data_federica_1990_2014_west.dta", clear //

gen quarter = q - 4 * (a - 1)
label var a "index of year"
label var year "year"
label var st_reg "stock regular workers"
label var founded_q "Quarter in which establishment founded"
label var quarter "Quarter (1-4)"
label var district "district (Kreis) of establishment"

rename st_reg employment

* Create time variable:
gen eventdate=yq(year,quarter)
format eventdate %tq
drop year quarter

rename w93_imp w93_3
sum employment 
count if employment==.
count if employment==.z
tab eventdate if employment==.
********************************************************************************
* Drop public sector & education 
drop if w93_3>=750 & w93_3<900
********************************************************************************
* Drop agriculture and mining:
drop if w93_3<150
********************************************************************************
qui: ren district krs
qui: destring krs, force replace

merge m:1 krs using "$data_add/amr_krs_72.dta"
drop if _merge!=3
drop _merge
********************************************************************************
rename betnr estid 
sum
gen logsize = ln(employment)
sum
by amr eventdate w93_3, sort: egen ct = mean(logsize)
replace logsize = logsize - ct
by estid, sort: egen c = count(logsize)

preserve
keep estid c
duplicates drop
sum c, detail
restore

sum amr
forv n = 1(1)`r(max)'{
preserve
keep if amr==`n'
xtset estid eventdate
reg logsize l.logsize
mat A=e(b)
gen coef1=A[1,1]
reg logsize l.logsize //  a_* , noconstant
mat A=e(b)
gen coef2=A[1,1]
reg logsize l.logsize [weight=employment]
mat A=e(b)
gen coef3=A[1,1]
reg logsize l.logsize [weight=employment] // a_* , noconstant
mat A=e(b)
gen coef4=A[1,1]
keep coef* amr
duplicates drop
if `n'==1{
save "$data_proc/temp.dta", replace
}
else{
append using "$data_proc/temp.dta"
save "$data_proc/temp.dta", replace
}
restore
}
use "$data_proc/temp.dta", clear
saveold "$data_proc/firmlevelpersistence.dta", replace version(14)
cap erase "$data_proc/temp.dta"
********************************************************************************
log close
********************************************************************************
